package cn.lg.soar.flow.api.model.node;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.flow.api.enums.NodeStatusEnum;
import cn.lg.soar.flow.api.utils.XmlUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Data
public class ServiceNode implements Node, Serializable {

    private String key;
    private String name;
    private String description;
    private NodeStatusEnum status;

    private List<ProcessListener> processListeners;

    // Getters and Setters
    @JsonProperty("type")
    @Override
    public String getType() { return "service"; }

    @Override
    public String toXmlString() {
        StringBuilder sb = new StringBuilder();

        // 开始生成 ServiceTask 标签
        sb.append("    <serviceTask id=\"").append(key).append("\" ");
        sb.append("name=\"").append(XmlUtils.escapeXml(getName())).append("\" ");

        // 可以在这里添加其他属性，如 flowable:class, flowable:expression, flowable:delegateExpression 等
        // 例如：service implementation
//        if (DataUtil.isValuable(implementation)) {
//            sb.append("flowable:").append(implementationType)
//                    .append("=\"").append(XmlUtils.escapeXml(implementation)).append("\" ");
//        }
        sb.append(">\n");
        // 添加执行监听器
        if (DataUtil.isValuable(processListeners)) {
            sb.append("<extensionElements>");
            if (DataUtil.isValuable(processListeners)) {
                for (ProcessListener x : processListeners) {
                    sb.append("<flowable:executionListener event=\"").append(x.getEvent()).append("\" ")
                            .append(Boolean.TRUE.equals(x.getIsExpression()) ? "expression" : "class")
                            .append("=\"").append(XmlUtils.escapeXml(x.getValue())).append("\" />\n");
                }
            }
            sb.append("</extensionElements>\n");
        }
        // 结束 serviceTask 标签
        sb.append("    </serviceTask>\n");
        return sb.toString();
    }
}